/**
 * 系统平台公共js
*/
layui.use(['layer', 'form','upload', 'laydate'], function() {

	var layer = layui.layer;
	var form = layui.form;
	var laydate = layui.laydate;
	var upload = layui.upload;
	/**
	 * 通用日期时间选择
	 */
	laydate.render({
		elem: '.datetime',
		type: 'datetime',
		format: 'yyyy-MM-dd HH:mm'
	});

	/**
	 * 通用表单提交(AJAX方式)
	 */

	form.on('submit(submit)', function(data) {

		var loading = layer.load(0, {
			shade: false
		}); //0代表加载的风格，支持0-2

		$.ajax({
			url: data.form.action,
			type: data.form.method,
			data: $(data.form).serialize(),
			success: function(info) {
				layer.close(loading); //关闭加载层
				//判断如果为弹窗提交则执行父类操作
				var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
				if (index) {
					parent.layer.close(index);
					parent.layer.msg(info.msg);
					parent.location.reload();
				} else {
					if (info.code === 1) {
						setTimeout(function() {
							location.href = info.url;
						}, 1000);
						showSuccess(info.msg);
					} else {
						showError(info.msg);
					}
				}
			},
			complete: function() {
				//若存在验证码则回调
				if (data.field.verify) {
					$("#verify").click();
				}
			}
		});
		return false;
	});



	/**
	 * 通用get - ajax提交
	 */
	$('.ajax-get').on('click', function() {
		var _href = $(this).attr('href');
		var _data = $(this).data('param');
		var loading = layer.load(0, {
			shade: false
		});
		$.ajax({
			url: _href,
			type: "get",
			data: _data,
			success: function(info) {
				console.log(info);
				layer.close(loading);
				//判断如果为弹窗提交则执行父类操作
				var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
				if (index) {
					parent.layer.close(index);
					parent.layer.msg(info.msg);
					parent.location.reload();
				} else {
					if (info.code === 1) {
						if(info.url){
							setTimeout(function() {
								location.href = info.url;
							}, 1000);
						}
						showSuccess(info.msg);
					} else {
						showError(info.msg);
					}
				}
			}
		});
		return false;
	});

	/**
	 * 通用post - ajax提交
	 */
	$('.ajax-post').on('click', function() {
		var _href = $(this).attr('href');
		var _data = $(this).attr('data-param');
		var loading = layer.load(0, {
			shade: false
		}); //0代表加载的风格，支持0-2
		$.ajax({
			url: _href,
			type: "post",
			data: _data,
			success: function(info) {
				layer.close(loading);
				//判断如果为弹窗提交则执行父类操作
				var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
				if (index) {
					parent.layer.close(index);
					parent.layer.msg(info.msg);
					parent.location.reload();
				} else {
					if (info.code === 1) {
						if(info.url){
							setTimeout(function() {
								location.href = info.url;
							}, 1000);
						}
						showSuccess(info.msg);
					} else {
						showError(info.msg);
					}
				}
			}
		});
		return false;
	});

	/**
	 * 通用删除
	 */
	$('.ajax-delete').on('click', function() {
		var _href = $(this).attr('href');
		layer.open({
			shade: false,
			content: '确定删除？',
			btn: ['确定', '取消'],
			yes: function(index) {
				var loading = layer.load(0, {
					shade: false
				}); //0代表加载的风格，支持0-2
				$.ajax({
					url: _href,
					type: "get",
					success: function(info) {
						layer.close(loading);
						//判断如果为弹窗提交则执行父类操作
						var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
						if (index) {
							parent.layer.close(index);
							parent.layer.msg(info.msg);
							parent.location.reload();
						} else {
							if (info.code === 1) {
								if(info.url){	
									setTimeout(function() {
										location.href = info.url;
									}, 1000);
								}
								showSuccess(info.msg);
							} else {
								showError(info.msg);
							}
						}
					}
				});
				layer.close(index);
			}
		});
		return false;
	});

	/**
	 * 通用状态修改
	 */
	$('.ajax-change').on('click', function() {
		var _href = $(this).attr('data-href');
		var _data = $(this).attr('data-param');
		var loading = layer.load(0, {
			shade: false
		});
		var this_class = $(this);
		$.ajax({
			url: _href,
			type: "post",
			data: _data,
			success: function(info) {
				layer.close(loading);
				if (info.code === 1) {
					this_class.toggleClass("layui-form-onswitch");
				} else {
					showError(info.msg);
				}
			}
		});
		return false;
	});
	/**
	 * 通用确认操作
	 */
	$('.ajax-confim').on('click', function() {
		var _href = $(this).attr('href');
		layer.open({
			shade: false,
			content: '确认操作？',
			btn: ['确定', '取消'],
			yes: function(index) {
				var loading = layer.load(0, {
					shade: false
				}); //0代表加载的风格，支持0-2
				$.ajax({
					url: _href,
					type: "get",
					success: function(info) {
						layer.close(loading);
						if (info.code === 1) {
							if(info.url){	
								setTimeout(function() {
									location.href = info.url;
								}, 1000);
							}
							showSuccess(info.msg);
						} else {
							showError(info.msg);
						}
					}
				});
				layer.close(index);
			}
		});
		return false;
	});

	/**
	 * 通用批量处理（审核、取消审核、删除）
	 */
	$('.ajax-action').on('click', function() {
		var _action = $(this).data('action');
		layer.open({
			shade: false,
			content: '确定执行此操作？',
			btn: ['确定', '取消'],
			yes: function(index) {
				var loading = layer.load(0, {
					shade: false
				}); //0代表加载的风格，支持0-2
				$.ajax({
					url: _action,
					data: $('.ajax-form').serialize(),
					success: function(info) {
						layer.close(loading);
						if (info.code === 1) {
							if(info.url){	
								setTimeout(function() {
									location.href = info.url;
								}, 1000);
							}
							showSuccess(info.msg);
						} else {
							showError(info.msg);
						}
					}
				});
				layer.close(index);
			}
		});

		return false;
	});


	/**
	 * 通用全选
	 */
	form.on('checkbox(checkAll)', function(data) {
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
		child.each(function(index, item) {
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});


	//成功提示

	function showSuccess(msg) {
		layer.msg(msg, {
			icon: 1,
			shade: 0.3,
			anim: 1
		});
	}
	//错误提示

	function showError(msg) {
		layer.msg(msg, {
			icon: 2,
			shade: 0.3,
			anim: 1
		});
	}

});



$('.ajax-upload-file').on('change', function() {
	var _url = $(this).data('url');
	var _dir = $(this).data('dir');
	var _allowext = $(this).data('allowext');
	var _ipt = $(this).data('ipt');
	var _src = $(this).data('src');
	var _purpose = $(this).data('purpose');
	var _thumb = $(this).data('thumb');

	var formData = new FormData();
	formData.append("file",$(this)[0].files[0]);
	formData.append("dir",_dir);
	formData.append("purpose",_purpose);
	formData.append("allowext",_allowext);
	formData.append("thumb",_thumb);

	$.ajax({
		url: _url,
		type: 'post',
		data: formData,
        contentType: false,
        processData: false,
		async: true,
		success: function(info) {
			//layer.close(loading);
			if (info.code == 1) {
				$(_ipt).val(info.url);
				if(_src){
					if(info.thumb){
						$(_src).attr('src',info.thumb);
					}else{
						$(_src).attr('src',info.url);
					}
				}
			} else {
				layer.msg(info.message, {
					icon: 2,
					shade: 0.3,
					anim: 1
				});
			}
		},
        error:function(info){
            console.log(info);
			layer.msg('上传失败', {
				icon: 2,
				shade: 0.3,
				anim: 1
			});

        }
	});


});

/**
 * 通用全选
 */
$('.check-all').on('click', function() {
	$(this).parents('table').find('input[type="checkbox"]').prop('checked', $(this).prop('checked'));
});
